package com.gong.bank.controller;

import com.gong.bank.model.UserBean;
import com.gong.bank.service.BankService;
import com.gong.bank.service.BankServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description:
 * @author:gong
 * @create: 4/8/2022 上午10:43
 */
public class MoneyController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String addMoney = req.getParameter("addMoney");
        String add = req.getParameter("add");
        String reduceMoney = req.getParameter("reduceMoney");
        String reduce = req.getParameter("reduce");
        String usernameadd = req.getParameter("usernameadd");
        String moneyadd = req.getParameter("moneyadd");
        String zhuanzhang = req.getParameter("zhuanzhang");
        BankService service = new BankServiceImpl();
        UserBean userInfo = (UserBean) req.getSession().getAttribute("userInfo");
        if (add!=null){
            if (Integer.parseInt(addMoney)<=0){
                req.getRequestDispatcher("error.jsp").forward(req,resp);
            }
            service.saveMoney(Double.parseDouble(addMoney),userInfo);
            service.insertLog(userInfo,add, Double.parseDouble(addMoney));
            UserBean userByName = service.findUserByName(userInfo.getUsername());
            req.setAttribute("user",userByName);
            req.getRequestDispatcher("main.jsp").forward(req,resp);
        }
        if (reduce!=null){
            if (Integer.parseInt(reduceMoney)<=0){
                req.getRequestDispatcher("error.jsp").forward(req,resp);
            }
            service.deleteMoney(Double.parseDouble(reduceMoney),userInfo);
            service.insertLog(userInfo,reduce, Double.parseDouble(reduceMoney));
            UserBean userByName = service.findUserByName(userInfo.getUsername());
            req.setAttribute("user",userByName);
            req.getRequestDispatcher("main.jsp").forward(req,resp);
        }
        if (zhuanzhang!=null){
            if (Integer.parseInt(moneyadd)<=0){
                req.getRequestDispatcher("error.jsp").forward(req,resp);
            }
            service.saveMoney(Double.parseDouble(moneyadd),service.findUserByName(usernameadd));
            service.deleteMoney(Double.parseDouble(moneyadd),userInfo);
            service.insertLog(userInfo,zhuanzhang, Double.parseDouble(moneyadd));
            UserBean userByName = service.findUserByName(userInfo.getUsername());
            req.setAttribute("user",userByName);
            req.getRequestDispatcher("main.jsp").forward(req,resp);
        }



    }
}
